#!/bin/bash
## mysql8的源码安装
HOME="/home"
VERSION="8.0.28"
MYSQL_FILES="mysql-${VERSION}.tar.gz"
MYSQL_FILES_DIR="mysql-${VERSION}"
# MYSQL_URL="http://mirrors.sohu.com/mysql/MySQL-8.0/$MYSQL_FILES"
MYSQL_URL="https://mirrors.aliyun.com/mysql/MySQL-8.0/$MYSQL_FILES"
MYSQL_BOOST_URL="https://mirrors.aliyun.com/mysql/MySQL-8.0/mysql-boost-${VERSION}.tar.gz"
MYSQL_INSTALL_TEMP="/usr/local"
MYSQL_INSTALL_PREFIX="${MYSQL_INSTALL_TEMP}/mysql8"
#mysql的放数据库的地方
MYSQL_DATA_DIR="/data/mysql"
##### cmake3.6.x版本
RESOURCE="/home/tools"
VERSION_CMAKE="cmake-3.6.2"
DOWNLOADURL="https://cmake.org/files/v3.6/${VERSION_CMAKE}.tar.gz"
#####
# 升级gcc的环境
function initEnv() {
    rpm -q centos-release-scl &>/dev/null
    if [ $? -ne 0 ]; then
        yum install centos-release-scl -y
    fi
    rpm -q devtoolset-10-gcc &>/dev/null
    if [ $? -ne 0 ]; then
        yum install devtoolset-10-gcc -y
    fi
    rpm -q devtoolset-10-gcc-c++ &>/dev/null
    if [ $? -ne 0 ]; then
        yum install devtoolset-10-gcc-c++ -y
    fi
    rpm -q devtoolset-11-binutils &>/dev/null
    if [ $? -ne 0 ]; then
        yum install devtoolset-10-binutils -y
    fi
   # scl enable devtoolset-11 bash # 临时启用gcc的11版本(关闭ssh连接gcc版本就还原)
   # exit 1
}

# 升级cmake版本
function buildCMake() {
    if [ ! -d "${RESOURCE}/${VERSION_CMAKE}" ]; then
        cd ${RESOURCE}
        tar -zxvf ${VERSION_CMAKE}.tar.gz
    fi
    cd "${RESOURCE}/${VERSION_CMAKE}"
    ./bootstrap
    gmake && gmake install
    if [ $? -eq 0 ]; then
        ln -sf /usr/local/bin/cmake /usr/bin/cmake3
        cmake3 --VERSION_CMAKE
        echo "cmake安装成功"
    else
        echo "cmake安装失败"
        exit 1
    fi
}

#创建用户及用户组方法
function createUserGroupAndUser() {
    group=$1
    user=$2
    egrep "^$group" /etc/group >&/dev/null
    #创建用户组
    if [[ $? -ne 0 ]]; then
        groupadd $group
    fi
    #创建用户组下面的用户
    egrep "^$user" /etc/passwd >&/dev/null
    if [ $? -ne 0 ]; then
        useradd -r -s /sbin/nologin -g $group $user
    fi
}

function decompressBoostDeal() {
    if [ ! -d ${MYSQL_INSTALL_TEMP}/mysql-${VERSION}-boost ]; then
        tar -xzvf mysql-boost-${VERSION}.tar.gz -C ${MYSQL_INSTALL_TEMP} && mv ${MYSQL_INSTALL_TEMP}/mysql-${VERSION} ${MYSQL_INSTALL_TEMP}/mysql-${VERSION}-boost
    fi
}

function decompressBoost() {
    cd "$HOME"
    if [ ! -f "$HOME/mysql-boost-${VERSION}.tar.gz" ]; then
        wget -c $MYSQL_BOOST_URL
        decompressBoostDeal
    else
        decompressBoostDeal
    fi
}

function updateZlib() {
    #https://blog.csdn.net/alvinlyb/article/details/103755600
    cd "$HOME"
    if [ ! -f "$HOME/zlib-1.2.13.tar.gz" ]; then
        wget -c https://zlib.net/zlib-1.2.13.tar.gz
    fi
    if [ ! -d "$HOME/zlib-1.2.13" ]; then
        tar -zxvf zlib-1.2.13.tar.gz
        cd zlib-1.2.13
        ./configure --libdir=/lib64/
        make && make install
        if [ $? -eq 0 ]; then
            rpm -qa | grep zlib | xargs rpm -e --nodeps
            rm -f /lib64/libz.so.1.2.7
            echo "zlib升级到1.2.13成功"
        else
            echo "zlib升级到1.2.13失败"
            exit 0
        fi
    fi
}

function installMysql() {
    rpm -q tar &>/dev/null
    if [ $? -ne 0 ]; then
        yum install tar -y
    fi
    rpm -q gcc &>/dev/null
    if [ $? -ne 0 ]; then
        yum install gcc -y
    fi
    rpm -q gcc-c++ &>/dev/null
    if [ $? -ne 0 ]; then
        yum install gcc-c++ -y
    fi
    rpm -q openssl &>/dev/null
    if [ $? -ne 0 ]; then
        yum install openssl -y
    fi
    rpm -q openssl-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install openssl-devel -y
    fi
    rpm -q autoconf &>/dev/null
    if [ $? -ne 0 ]; then
        yum install autoconf -y
    fi
    rpm -q wget &>/dev/null
    if [ $? -ne 0 ]; then
        yum install wget -y
    fi
    rpm -q cmake &>/dev/null
    if [ $? -ne 0 ]; then
        yum install cmake -y
    fi
    rpm -q ncurses &>/dev/null
    if [ $? -ne 0 ]; then
        yum install ncurses -y
    fi
    rpm -q ncurses-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install ncurses-devel -y
    fi
    rpm -q ncurses-libs &>/dev/null
    if [ $? -ne 0 ]; then
        yum install ncurses-libs -y
    fi
    rpm -q libaio &>/dev/null
    if [ $? -ne 0 ]; then
        yum install libaio -y
    fi

    createUserGroupAndUser mysql mysql
    # 创建空文件夹
    if [ ! -d "$HOME" ]; then
        mkdir -p $HOME
    fi
    if [ ! -d "${MYSQL_DATA_DIR}" ]; then
        mkdir -p $MYSQL_DATA_DIR
    fi
    if [ ! -d "${RESOURCE}" ]; then
        mkdir -p ${RESOURCE}
    fi
    # 升级cmake版本
    if [ ! -f "${RESOURCE}/${VERSION_CMAKE}.tar.gz" ]; then
        cd ${RESOURCE}
        wget ${DOWNLOADURL}
        buildCMake
    else
        buildCMake
    fi
    initEnv #gcc的高版本编译环境
    # 处理mysql-boost
    decompressBoost
    # 升级zlib为1.2.13
    updateZlib

    cd "$HOME"
    if [ ! -f "$HOME/$MYSQL_FILES" ]; then
        wget -c $MYSQL_URL && tar -xzvf $MYSQL_FILES
    fi
    if [ ! -d "$HOME/$MYSQL_FILES_DIR" ]; then
        tar -xzvf $MYSQL_FILES
    fi
    cd $HOME/$MYSQL_FILES_DIR

    cmake3 . -DCMAKE_INSTALL_PREFIX=$MYSQL_INSTALL_PREFIX \
    -DMYSQL_DATADIR=${MYSQL_DATA_DIR}/data \
    -DDEFAULT_CHARSET=utf8mb4 \
    -DDEFAULT_COLLATION=utf8mb4_general_ci \
    -DEXTRA_CHARSETS=all \
    -DENABLED_LOCAL_INFILE=ON \
    -DWITH_INNODB_MEMCACHED=ON \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_FEDERATED_STORAGE_ENGINE=1 \
    -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
    -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
    -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
    -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
    -DCOMPILATION_COMMENT="binghe edition" \
    -DDOWNLOAD_BOOST=1 \
    -DWITH_BOOST=${MYSQL_INSTALL_TEMP}/mysql-${VERSION}-boost \
    -DMYSQL_UNIX_ADDR=${MYSQL_DATA_DIR}/run/mysql.sock \
    -DMYSQL_TCP_PORT=3306 \
    -DSYSCONFDIR=/etc \
    -DWITH_READLINE=1 \
    -DFORCE_INSOURCE_BUILD=1 \
    -DWITH_SSL=system \
    -DWITH_ZLIB=system \
    #-DCMAKE_CXX_COMPILER=/usr/local/gcc-5.3.0/bin/g++ \
    #-DCMAKE_C_COMPILER=/usr/local/gcc-5.3.0/bin/gcc\

    gmake && gmake install
    if [ $? -eq 0 ]; then
        #msyql的开机启动
        /bin/cp support-files/mysql.server /etc/init.d/mysqld
        chmod +x /etc/init.d/mysqld
        chkconfig –-add /etc/init.d/mysqld
        chkconfig mysqld on
        #初始化数据库
        cd $MYSQL_INSTALL_PREFIX && mkdir -p ${MYSQL_DATA_DIR}/data && chown -R mysql:mysql ./ && ./bin/mysqld --initialize --user=mysql --basedir=${MYSQL_DATA_DIR}/ --datadir=${MYSQL_DATA_DIR}/data
        #软链接
        ln -s $MYSQL_INSTALL_PREFIX/bin/mysql /usr/bin/mysql
        echo -e "\n\033[32m———————————————–\033[0m"
        echo -e "\033[32mServer Install Success !\033[0m"
        echo -e "\033[32m在/etc下创建my.cnf的文件,内容在文档的下面\033[0m"
        echo -e "\033[32mvim /etc/init.d/mysqld\033[0m"
        echo -e "\033[32mbasedir=$MYSQL_INSTALL_PREFIX\033[0m"
        echo -e "\033[32mdatadir=${MYSQL_DATA_DIR}/data\033[0m"
        echo -e "\033[32mservice mysqld stop|restart|start|status\033[0m"
    else
        echo -e "\033[32mMake or Make install ERROR,(rm CMakeCache.txt解决安装依赖重新安装)\033[0m"
        exit 0
    fi
}

installMysql
# : <<!
# ### my.cnf的配置文件
# [client]
# port = 3306
# socket = ${MYSQL_DATA_DIR}/run/mysql.sock
# default-character-set = utf8mb4

# [mysql]
# no-auto-rehash
# default-character-set=utf8mb4
# net-buffer-length=64K
# unbuffered
# max-allowed-packet = 2G
# default-character-set = utf8mb4

# [mysqld]
# #skip-name-resolve
# #设置3306端口
# port = 3306
# socket=${MYSQL_DATA_DIR}/run/mysql.sock
# # 设置mysql的安装目录
# basedir=/usr/local/mysql
# # 设置mysql数据库的数据的存放目录
# datadir=${MYSQL_DATA_DIR}/data
# tmpdir = ${MYSQL_DATA_DIR}/tmp
# # 允许最大连接数
# max_connections=3000
# # 服务端使用的字符集默认为8比特编码的latin1字符集
# character-set-server=utf8mb4
# # 创建新表时将使用的默认存储引擎
# default-storage-engine=INNODB
# #lower_case_table_name=1
# max_allowed_packet=16M

# [mysqldump]
# quick
# max_allowed_packet=2G
# net_buffer_length=8k
# !
